#!/bin/bash
# 
# 部署pgAdmin
#


#变量说明
svc_user_name=pgadmin  #服务账号
svc_user_group=$svc_user_group
pgadmin_pkg_url=https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v2.1/pip/pgadmin4-2.1-py2.py3-none-any.whl
pgadmin_pkg_name=pgadmin4-2.1-py2.py3-none-any.whl
pgadmin_env=/usr/local/pgadmin  #虚拟环境 工作目录
system_version=$(cat /proc/version)
uwsgi_port=9000
uwsgi_stat_port=9191
uwsgi_process_number=1
uwsgi_thread_number=2
pgadmin_svc_path=/usr/lib/systemd/system/pgadmin.service
system_fc="fedora"
system_ubuntu="ubuntu"
uwsgi_log_path=/var/log/pgadmin/uwsgi.log
uwsgi_log_max_size=52428800 #5mb
#检查系统版本
echo -e "\033[40;32m =============检查系统版本============= \033[0m" 
if [[ $system_version =~ $system_fc ]]
then 
	pkg_tool=dnf
	echo "this system is $system_fc"
	sudo dnf install pcre pcre-devel python3
elif [[ $system_version =~ $system_ubuntu ]]
then
	pkg_tool=apt-get
	echo "this system is $system_ubuntu"
	sudo apt-get install libpcre3 libpcre3-dev python3
else
	echo "system is not support"
	echo -e "\033[40;31m system is not support \033[0m" 
	return
fi

sudo pip3 install virtualenv


#创建服务账户
#判断是否 已经创建
echo -e "\033[40;32m =============创建服务账户============= \033[0m" 

user_list=$(cat /etc/passwd)
if [[ $user_list =~ "$svc_user_name:" ]]
then
	echo "$svc_user_name 已经存在，无需创建"
else
	sudo useradd $svc_user_name -s /usr/sbin/nologin
	sudo passwd $svc_user_name
fi
#创建虚拟环境
echo -e "\033[40;32m ============创建虚拟环境=============== \033[0m" 
sudo mkdir $pgadmin_env
sudo chown $USER $pgadmin_env
virtualenv $pgadmin_env 
source $pgadmin_env/bin/activate
pip uninstall uwsgi
pip install uwsgi  --no-cache-dir

#安装pgadmin4
echo -e "\033[40;32m ============安装pgadmin4=============== \033[0m" 
pip install $pgadmin_pkg_url
sudo chown $svc_user_name -R $pgadmin_env
sudo mkdir /var/lib/pgadmin
sudo mkdir /var/log/pgadmin
sudo chown  $USER  -R /var/lib/pgadmin
sudo chown  $USER  -R /var/log/pgadmin

py_version=$(ls $pgadmin_env/lib)
echo -e "\033[40;32m ============初始化gadmin4=============== \033[0m" 
pgadmin_work_dir=$pgadmin_env/lib/$py_version/site-packages/pgadmin4/ #运行目录
python $pgadmin_work_dir/pgAdmin4.py
sudo chown  $svc_user_name  -R /var/lib/pgadmin
sudo chown  $svc_user_name  -R /var/log/pgadmin
pip install psycopg2-binary


#创建uwsgi配置文件
echo -e "\033[40;32m ============创建uwsgi配置文件=============== \033[0m"
cat<<EOF | sudo tee $pgadmin_env/pgadmin.ini
[uwsgi]
home=$pgadmin_env
http-socket= 127.0.0.1:$uwsgi_port
chdir = $pgadmin_work_dir
wsgi-file =pgAdmin4.wsgi
processes = $uwsgi_process_number
threads = $uwsgi_thread_number
stats = 127.0.0.1:$uwsgi_stat_port
uid=$svc_user_name
logger=file:logfile=$uwsgi_log_path,maxsize=$uwsgi_log_max_size
EOF

#创建pgadmin.service
echo -e "\033[40;32m ============pgadmin.service=============== \033[0m"
cat<<EOF | sudo tee $pgadmin_svc_path
[Unit]

Description=pgAdmin

After=postgresql.service

[Service]

Type=notify

KillSignal=SIGQUIT

User=$svc_user_name

Group=$svc_user_group

ExecStart=$pgadmin_env/bin/uwsgi --ini $pgadmin_env/pgadmin.ini

[Install]

WantedBy=multi-user.target

EOF


sudo systemctl enable $pgadmin_svc_path
sudo systemctl start pgadmin
sudo systemctl status pgadmin








